package com.jcl.recruit.console.recruit.logic;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.constants.Constant;
import com.jcl.core.db.page.DistributePageTotal;
import com.jcl.core.db.page.model.PageCondition;
import com.jcl.core.db.page.model.PageTotal;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.console.db.logic.DbServerLogic;
import com.jcl.recruit.log.dao.ConsoleResumeLogDao;
import com.jcl.recruit.log.model.ConsoleResumeLog;
import com.jcl.recruit.log.vo.LogPageCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName consoleRecruitLogPage
 * @Author yyliu
 * @Date 2018/12/24 17:52
 * @Version 1.0
 **/
@Component("consoleResumeLogPage")
public class ConsoleResumeLogPage implements DistributePageTotal<ConsoleResumeLog> {

    @Autowired
    private ConsoleResumeLogDao consoleResumeLogDao;

    @Autowired
    private DbServerLogic dbServerLogic;

    @Override
    public PageTotal pageTotal(PageCondition condition) {
        List<String> dbServerNameList = dbServerLogic.getServerNameList();
        PageTotal pageTotal = new PageTotal();
        Long total = 0L;
        List<String> serverList = new ArrayList<>();
        Map<String, Integer> serverCount = new HashMap<>();
        if (CollectionUtils.isNotEmpty(dbServerNameList)) {
            for (String dbName : dbServerNameList) {
                if(dbName.equals(Constant.ELASTIC_DB_NAME)){
                    continue;
                }
                int tempCount = 0;
                List<Map<String, Object>> tempList = consoleResumeLogDao.consoleFindCount(dbName, (LogPageCondition) condition);
                if (CollectionUtils.isNotEmpty(tempList)) {
                    Map<String, Object> temp = tempList.get(0);
                    if (null != temp.get("number")) {
                        tempCount = Integer.valueOf(temp.get("number").toString());
                    }
                    total += tempCount;
                }
                if (tempCount > 0) { // 去除无效的数据库查询
                    serverCount.put(dbName, tempCount);
                    serverList.add(dbName);
                }
            }
        }
        pageTotal.setTotal(total);
        pageTotal.setServerList(serverList);
        pageTotal.setServerCount(serverCount);
        return pageTotal;
    }

    @Override
    public PageResult pageResult(PageCondition condition, Map<String, Integer[]> everyServerCurPageCount) {
        List<ConsoleResumeLog> logList = new ArrayList<>();
        if (null != everyServerCurPageCount && everyServerCurPageCount.size() > 0) {
            for (Map.Entry<String, Integer[]> entity : everyServerCurPageCount.entrySet()) {
                List<ConsoleResumeLog> tempList = consoleResumeLogDao.findList(entity.getKey(), (LogPageCondition) condition,
                        entity.getValue()[0], entity.getValue()[1]);
                logList.addAll(tempList);
            }
        }
        PageResult result = new PageResult();
        result.setData(logList);
        return result;
    }

    @Override
    public List<ConsoleResumeLog> comparator(List<ConsoleResumeLog> data) {
        Collections.sort(data, new Comparator<ConsoleResumeLog>() {
            @Override
            public int compare(ConsoleResumeLog o1, ConsoleResumeLog o2) {
                // ResumeLog 降序排列
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return data;
    }

}
